<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/10/20
 * Time: 18:13
 */
class FindpasswdController extends Controller {

    private $_time;

    public function init() {
        parent::init();
        $this->_time = time();
    }

    public function indexAction(){

    }

    public function steptowAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['code'] = empty($Post['code']) ? '' : htmlspecialchars($Post['code'], ENT_QUOTES, 'UTF-8');
            $Post['mobile'] = empty($Post['mobile']) ? '' : htmlspecialchars($Post['mobile'], ENT_QUOTES, 'UTF-8');
            $Post['codetime'] = empty($Post['codetime']) ? '' : htmlspecialchars($Post['codetime'], ENT_QUOTES, 'UTF-8');

            $code = $this->_session['captcha'];
            $codetime = $this->_session['codetime'];

            if($code != $Post['code']){
                $this->redirect ( '/findpasswd/index' );
                die();
            }

            if($codetime  != $Post['codetime']){
                $this->redirect ( '/findpasswd/index' );
                die();
            }


            $user = new UserModel();

            $userinfo = $user->getUserByLoginItem($Post['mobile']);

            $this->_view->codetime = $codetime;
            $this->_view->userinfo = $userinfo;

        }
    }

    public function changepasswdAction(){
        if($this->_request->isPost()) {

            $Post = $this->_request->getPost();
            $Post['moblie'] = empty($Post['moblie']) ? null : htmlspecialchars(trim($Post['moblie']), ENT_QUOTES, 'UTF-8');
            $Post['codetime'] = empty($Post['codetime']) ? null : htmlspecialchars(trim($Post['codetime']), ENT_QUOTES, 'UTF-8');
            $codetime = $this->_session->offsetGet('codetime');

            if(empty($Post['moblie'])){
                $this->redirect ( '/findpasswd/index' );
                die();
            }

            if($codetime  != $Post['codetime']){
                $this->redirect ( '/findpasswd/index' );
                die();
            }

            $this->_view->moblie  =$Post['moblie'];
            $this->_view->codetime  =$Post['codetime'];

        }
    }

    public function confirmPhonecodeAction(){
        if($this->_request->isPost()) {
            global $_GlobalLanguageCN;
            $Post = $this->_request->getPost();
            $Post['code'] = empty($Post['code']) ? '' : htmlspecialchars($Post['code'], ENT_QUOTES, 'UTF-8');
            $Post['mobile'] = empty($Post['mobile']) ? '' : htmlspecialchars($Post['mobile'], ENT_QUOTES, 'UTF-8');

            $send = new SendsmsModel();
            $ret = $send->getRowByWhere("receiver ='{$Post['mobile']}' and code='{$Post['code']}' and send_time < '{$this->_time}' and (send_time+1800) > '{$this->_time}' and type = 'Findpassword' AND status=1");
            if(empty($ret)){
                $retdata = array(
                    'code' => '99',
                    'msg'  => $_GlobalLanguageCN[0],
                );
                echo json_encode($retdata);
                die();
            }else{
                $retdata = array(
                    'code' => '88',
                    'msg'  => $_GlobalLanguageCN[1],
                );
                echo json_encode($retdata);
                die();
            }
        }
        die();
    }

    public function sendcodeAction(){
        if($this->_request->isPost()) {
            global $_SmsSendSwitch,$_SmsSendContent;

            $Post = $this->_request->getPost();

            $Post['mobile'] = empty($Post['mobile']) ? '' : htmlspecialchars($Post['mobile'], ENT_QUOTES, 'UTF-8');
            $Post['codetime'] = empty($Post['codetime']) ? '' : htmlspecialchars($Post['codetime'], ENT_QUOTES, 'UTF-8');

            if(empty($Post['mobile'])){
                $ret_data="-5";
                echo json_encode($ret_data);
                die();
            }
            $codetime = $this->_session->offsetGet('codetime');
            if($Post['codetime'] != $codetime){
                $ret_data="-4";
                echo json_encode($ret_data);
                die();
            }

            if($_SmsSendSwitch['op_find'] =1) {

                $code = Tools::getRandNums(6);
                $content = str_replace('%d1',$code,$_SmsSendContent['op_find_c']);

                $data = array(
                    'code' => $code,
                    'mobile' => $Post['mobile'],
                    'content' => $content,
                );
                $dataType = "Findpassword";
                $ret = Sms::DatabaseSend($this->_db_handler, $data, $dataType);

                if (!$ret) {
                    $ret_data = "-6";
                    echo json_encode($ret_data);
                    die();
                }
                echo json_encode($ret);
                die();
            }

        }
        die();
    }

    public function updatepasswdAction(){

        if($this->_request->isPost()) {
            global $_GlobalLanguageCN;
            $Post = $this->_request->getPost();
            $Post['moblie'] = empty($Post['moblie']) ? null : htmlspecialchars(trim($Post['moblie']), ENT_QUOTES, 'UTF-8');
            $Post['password'] = empty($Post['password']) ? null : htmlspecialchars(trim($Post['password']), ENT_QUOTES, 'UTF-8');
            $Post['repassword'] = empty($Post['repassword']) ? null : htmlspecialchars(trim($Post['repassword']), ENT_QUOTES, 'UTF-8');
            $Post['codetime'] = empty($Post['codetime']) ? null : htmlspecialchars(trim($Post['codetime']), ENT_QUOTES, 'UTF-8');
            $codetime = $this->_session->offsetGet('codetime');

            if (empty($Post['moblie'])) {
                $retdata = array(
                    'code' => '33',
                    'msg' => $_GlobalLanguageCN[2],
                );
                echo json_encode($retdata);
                die();
            }
            if ($Post['password'] != $Post['repassword']) {
                $retdata = array(
                    'code' => '34',
                    'msg' =>  $_GlobalLanguageCN[3],
                );
                echo json_encode($retdata);
                die();
            }

            if (!Tools::checkPassword($Post['password'])) {
                $retdata = array(
                    'code' => '36',
                    'msg' =>  $_GlobalLanguageCN[4],
                );
                echo json_encode($retdata);
                die();
            }


            if ($Post['codetime'] == $codetime) {


                $user = new UserModel();
                $isExistUser = $user->isExistByRow('moblie', $Post['moblie']);
                if (empty($isExistUser)) {
                    Tools::echoJs("alert( $_GlobalLanguageCN[5]);parent.location ='/findpasswd/index'");
                    die();
                }

                $data = array(
                    'password' => md5($Post['password']),
                    'update_time' => date('Y-m-d H:i:s', $this->_time)
                );

                $ret = $user->updateByWhere("moblie='{$Post['moblie']}'",$data);

                if(!empty($ret)){
                    Tools::dieJs("alert('$_GlobalLanguageCN[6]');parent.location ='/login/'");
                    unset($_SESSION['codetime']);
                    die();
                }else{
                    Tools::dieJs("alert('$_GlobalLanguageCN[6]');parent.location ='/login/'");
                    unset($_SESSION['codetime']);
                    die();
                }

            } else {
                Tools::echoJs("alert('$_GlobalLanguageCN[7]');parent.location ='/findpasswd/index'");
                die();
            }
        }

    }
}
